package com.liumuxian.video.service.impl;

import com.liumuxian.video.dao.ResourceDao;
import com.liumuxian.video.pojo.Resource;
import com.liumuxian.video.service.ResourceService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.IdWorker;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
@Transactional
public class ResourceServiceImpl implements ResourceService {
    @javax.annotation.Resource
    private ResourceDao resourceDao;
    @javax.annotation.Resource
    private IdWorker idWorker;

    @Override
    public void save(Resource resource) {
        resource.setId(idWorker.nextId()+"");
        resourceDao.save(resource);
    }
    @Override
    public void saveAll(List<Resource> resources) {
        List<Resource> resourceList = new ArrayList<>();
        resources.forEach(l -> {
            Resource resource = new Resource();
            resource.setId(idWorker.nextId()+"");
            resource.setCreatdata(new Date());
            resourceList.add(resource);
        });

        resourceDao.saveAll(resourceList);
    }

    @Override
    public void update(Resource resource){
        resourceDao.update(resource.getName(),resource.getState(),resource.getCreatdata(),
        resource.getUpdatedate(),resource.getId());
    }

    @Override
    public List<Resource> findAllByState(Integer state) {
        return resourceDao.findAllByState(state);
    }

    @Override
    public Resource getOne(String rid) {
        return resourceDao.getOne(rid);
    }
}
